package org.flyframework.event
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.controls.Alert;
	import mx.core.IMXMLObject;
	
	
	[Event(name="configured")]
	
	[DefaultProperty(name="mappings")] 
	
	
	/**
	 * Event Controller...
	 * 
	 * @author Ben Edwards 
	 */
	public class Controller extends EventDispatcher implements IMXMLObject
	{
		
		[ArrayElementType("org.flyframework.event.EventMapping")] 
		public var mappings:Array = new Array();
		
		
		//--------------------------------------------------------------------------
		// Init
		//--------------------------------------------------------------------------
		
		public function Controller()
		{
			
		}
		
		
		//--------------------------------------------------------------------------
		// Init
		//--------------------------------------------------------------------------
		
		protected function configure():void
		{
			trace( "Controller configuring "+this.mappings.length+" mappings." );
			
			var mapping:EventMapping;
			for each( mapping in this.mappings )
			{
				mapping.controller = this;
				
				/*
				if( mapping.listenTo == null )
				{
					mapping.listenTo = this;
				}
				
				if( mapping.listenFor == null )
				{
					throw new Error( "EventMapping requires a 'listenFor' event type attribute value." );
				}
				
				if( mapping.handler != null )
				{
					mapping.listenTo.addEventListener( mapping.listenFor, mapping.handler );
				}
				*/
			}
			
			dispatchEvent( new Event("configured") );
		}
		
		/**
		 * Remove all mappings.
		 */
		public function clear():void
		{
			trace( "Controller removing "+this.mappings.length+" mappings." );
			
			var mapping:EventMapping;
			for each( mapping in this.mappings )
			{
				
			}
		}
		
		
		//--------------------------------------------------------------------------
		// IMXMLObject
		//--------------------------------------------------------------------------
		
		public function initialized( document:Object, id:String ):void
		{
			trace( "Controller.initialized(): " + this.mappings.length );
			
			configure();
		}
		
		
		//--------------------------------------------------------------------------
		// 
		//--------------------------------------------------------------------------
		
		
	}
}